package cn.com.header.web.service.impl.sys;


import cn.com.header.core.util.*;
import cn.com.header.web.mapper.sys.UserDAO;
import cn.com.header.web.mapper.sys.UserRoleDAO;
import cn.com.header.web.model.sys.Role;
import cn.com.header.web.model.sys.User;
import cn.com.header.web.model.sys.UserRole;
import cn.com.header.web.service.sys.UserService;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 用户Service实现类
 *
 * @author StarZou
 * @since 2014年7月5日 上午11:54:24
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDao;


    @Override
    public User authentication(User user) {
        User u = userDao.authentication(user);
        return u;
    }

    @Override
    public User selectByUsername(String username) {
        return userDao.selectByUsername(username);
    }


    public Page selectByExampleAndPage(Page page) {
        page.setResult(userDao.selectByExampleAndPage(page));
        return page;
    }


    @Transactional
    public void saveUser(User user) {

        if (user.getId() == null) {
            userDao.insert(user);
        }
        //修改用户
        else {
            userDao.update(user);
        }
    }

    /**
     * 删除指定用户，同时删除其权限及其它资源
     */
    @Transactional
    public void deleteUser(Long id) {
        User user = userDao.get(id);
        user.setUserName(user.getUserName()+"_"+ LangUtils.uuidToString());
        user.setDelMark("1");
        userDao.update(user);
    }

    @Override
    public User get(Long id) {
        return userDao.get(id);
    }

    @Override
    public void update(User user) throws Exception {
        userDao.update(user);
    }

    @Override
    public User findAllByParams(Map<String, Object> map) throws Exception {
        return userDao.findAllByParams(map);
    }

    @Override
    public Boolean isNumberUnique(String number, Long id) {
        return userDao.isNumberUnique(number, id);
    }

    @Override
    public Page findByRoleId(Page page) {
        page.setResult(userDao.findByRoleId(page));
        return page;
    }

    @Override
    public boolean check(Map<String, Object> map) throws Exception {
        return userDao.check(map)==0;
    }

    @Override
    public String generateUserName(String name) {
        String regLoginName = userDao.generateUserName(name);
        if (userDao.selectByUsername(regLoginName) != null) {
            for (Integer i = 1; i < 10000; i++) {
                //找出一个不重复的用户名进行注册操作
                if (userDao.selectByUsername(regLoginName + i.toString()) == null) {
                    regLoginName = regLoginName + i.toString();
                    break;
                }
            }
        }
        return regLoginName;

    }

    @Override
    public User findByRelationUserId(String relationUserId) {
        return userDao.findByRelationUserId(relationUserId);
    }


}
